package com.wyh.controller;

import com.wyh.Mapper.ContractMapper;
import com.wyh.Mapper.ContractMapper;
import com.wyh.pojo.Contract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class ContractController {
    @Autowired
    ContractMapper contractMapper;


    @RequestMapping("/conts")
    public String list(Model model){
        List<Contract> all = contractMapper.getContractList();

        model.addAttribute("conts",all);
        return "cont/list";
    }
    @GetMapping("/contract")
    public String toAddPage(Model model){
        return "cont/add";
    }

    @PostMapping("/contract")
    public String addContract(Contract contract){
        contractMapper.addContract(contract);
        return "redirect:/conts";
    }

    @GetMapping("/toEditCont/{id}")
    public String toEdit(@PathVariable("id") Integer id, Model model){
        Contract contract = contractMapper.getContractById(id);
        model.addAttribute("cont",contract);
        return "cont/update";
    }

    @PostMapping("/updateCont")
    public String updateContract(Contract contract){
        contractMapper.updateContract(contract);
        return "redirect:/conts";
    }

    @GetMapping("/deleteCont/{id}")
    public String delete(@PathVariable("id") Integer id){
        contractMapper.deleteContract(id);
        return "redirect:/conts";
    }

    @RequestMapping("/searchCont")
    public String searchContract(String name, Model model){
        List<Contract> userList = contractMapper.getContractByName(name);
        model.addAttribute("conts",userList);
        return "cont/list";
    }
}
